package com.zkingsoft.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.WebArticleType;
import com.zkingsoft.service.WebArticleTypeService;

/**
 * @author 陈才
 * @date 2016-12-01 10:36
 */
@Controller
@RequestMapping(value = "admin/webArticleType")
public class WebArticleTypeController  {

	@Resource
	private WebArticleTypeService webArticleTypeService;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "WebArticleType_BEV";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebArticleType webArticleType, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<WebArticleType> dataList = webArticleTypeService.findInPage(webArticleType, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
				webArticleTypeService.findTotal(webArticleType));
		return result;
	}

	/**
	 * 加载所有列表
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all(WebArticleType webArticleType) {
		List<WebArticleType> list = webArticleTypeService.findByModel(webArticleType);
		if (webArticleType.getArtTypeId() != null && list.size() > 0) {
			WebUtil.setSessionAttribute("WebArticleType_BEV", list.get(0));
		}
		return new AjaxResult(AjaxResult.STATUS_SUCCESS,  list);
	}

	/**
	 * 新增或者修改页面
	 */
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebArticleType webArticleType) {
		if (webArticleType.getArtTypeId() != null) {
			webArticleTypeService.modifyByMap((WebArticleType) WebUtil.getSessionAttribute(this.BEV), webArticleType);
			AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, "文章类型修改成功");
			WebUtil.removeSessionAttribute(this.BEV);
			return result;
		} else {
			int i = webArticleTypeService.add(webArticleType);
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "文章");
		}
	}

	/**
	 * 进入修改界面
	 */
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		WebArticleType webArticleType;
		if (id != null) {
			webArticleType = webArticleTypeService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webArticleType);
			WebUtil.setSessionAttribute(this.BEV, webArticleType);
		}
		return "admin/web/articleType-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = strToCollToLong(keys, ",");
		int i = webArticleTypeService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i);
		} else {
			throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
		}
	}

	public static List<Long> strToCollToLong(String sour, String splitPatten) {
		List<Long> list = new ArrayList<>();
		if (sour != null) {
			String[] temps = sour.split(splitPatten);
			for (String string : temps) {
				list.add(Long.parseLong(string));
			}
		}
		return list;
	}

	/**
	 * 删除文章类型及其子类
	 */
	@RequestMapping(value = "/delAll")
	public @ResponseBody AjaxResult delAll(Long artTypeId) {
		int i = webArticleTypeService.removeById(artTypeId);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,   "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL,   "删除失败");
		}
	}
}